#include "CController.h"

CController::CController ()
{
	//MouseController::MouseController ();

	m_wTimer = 0;
	m_bIsZooming = false;
}

void CController::Init (FeetOnTerrain *ft, Camera* cam, BaseObject *hNode, BaseObject *vNode,
						float sen, WORD width, WORD height)
{
	m_FT = ft;
	m_camera = cam;
	m_sensitivity = sen;
	m_pHNode = hNode;
	m_pVNode = vNode;

	m_width = width;
	m_height = height;
}

void CController::LeftDrag(int x, int y)
{
	int	dx, dy;

	//MouseController::LeftDrag (x, y);

	dx = x - mOriX;
	dy = y - mOriY;

	//m_camera->moveUp(0.01f*m_sensitivity*dy);
	//m_camera->moveRight(0.01f*m_sensitivity*dx);

	m_pHNode->rotate(0.1f*dx, Vector3::UNIT_Y, GLOBAL);
	//m_pHNode->rotate(0.1f*dy, Vector3::UNIT_X, GLOBAL);
	

	//m_camera->rotate( 0.1f*dy, Vector3::UNIT_X, LOCAL);
	//m_camera->yaw(0.01f*dx);
	//m_camera->pitch(0.01f*dy);

	//m_root->translate (dx * 0.001f, dy * 0.001f, 0.0);
}

void CController::pressKey(char key)
{
	switch(key)
	{
	case 'w':
		m_FT->move(m_camera->localAxisInWorldSpace( Vector3::NEGATIVE_UNIT_Z), 5.0f);
		break;
	case 's':
		m_FT->move(m_camera->localAxisInWorldSpace( Vector3::UNIT_Z), 5.0f);
		break;
	case 'a':
		m_FT->move(m_camera->localAxisInWorldSpace( Vector3::NEGATIVE_UNIT_X), 5.0f);
		break;
	case 'd':
		m_FT->move(m_camera->localAxisInWorldSpace( Vector3::UNIT_X), 5.0f);
		break;
	}
}

void CController::RightDrag (int x, int y)
{
	int	dx, dy;

	//MouseController::RightDrag (x, y);

	dx = x - mOriX;
	dy = y - mOriY;

	m_pVNode->rotate(0.1f*dy, Vector3::UNIT_X, LOCAL);

	//m_root->rotate (dy * 0.01f, Vector3::NEGATIVE_UNIT_X, GLOBAL);
	//m_root->rotate (dx * 0.01f, Vector3::UNIT_Y, GLOBAL);
}

void CController::Wheel (short delta)
{
	m_bIsZooming = true;

	m_wTimer = 5;

	if (delta > 0)
		m_wZoomDir = 1;
	else
		m_wZoomDir = 0;
}

void CController::Zoom ()
{
	if (m_wZoomDir == 1)
		//m_camera->moveForward (0.1f);
		m_pVNode->translate(Vector3::UNIT_Z, LOCAL);
	else
		m_pVNode->translate(Vector3::NEGATIVE_UNIT_Z, LOCAL);

		//m_camera->moveForward ( -0.1f);

	if (--m_wTimer == 0)
		m_bIsZooming = false;
}

void CController::Render ()
{
	/*if (beLDrag () || beRDrag ())
	{
		Begin2D ();

		DrawLine2D (mOriX, mOriY, mWinX, mWinY);

		DrawCircle2D (mOriX, mOriY, 10.0f);

		End2D ();
	}*/
}

void CController::Begin2D ()
{
	//glDisable (GL_LIGHTING);

	glViewport (0, 0, m_width, m_height);

	glMatrixMode (GL_PROJECTION);
	glPushMatrix ();
	glLoadIdentity ();

	glOrtho (0, m_width, 0, m_height, 1.0, -1.0);

	glMatrixMode (GL_MODELVIEW);
	glPushMatrix ();
	glLoadIdentity ();
}

void CController::End2D ()
{
	glMatrixMode (GL_MODELVIEW);
	glPopMatrix ();

	glMatrixMode (GL_PROJECTION);
	glPopMatrix ();

	//glEnable (GL_LIGHTING);
}

void CController::DrawLine2D (WORD x1, WORD y1, WORD x2, WORD y2)
{
	glLineWidth (1.0);

	glBegin (GL_LINES);
		glVertex2i (x1, y1);
		glVertex2i (x2, y2);
	glEnd ();
}

void CController::DrawCircle2D (WORD x, WORD y, float radius)
{
	static float	pi = 3.1415926535f;
	float			angle;

	glBegin (GL_TRIANGLE_FAN);
	
	glVertex2f (x, y);

	for (angle = 0.0f; angle < 2.0f * pi; angle += (pi / 8.0f))
		glVertex2f (x + radius * sin (angle), y + radius * cos (angle));

	glVertex2f (x + 0.0f, y + radius);

	glEnd ();
}
